package com.xuebank.nfe.nfecar

import android.os.Build
import android.text.Html
import android.text.Spanned
import android.text.TextUtils
import android.view.View
import com.google.gson.Gson
import com.wzn.libaray.utils.CommonUtils
import com.wzn.libaray.utils.toast.ToastHelper
import com.xuebank.nfe.nfecar.model.base.BaseModel
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import java.io.File

/**
 * Created by Wind_Fantasy on 2017/7/10.
 */
/**
 * 任何地方都可以的toast
 */
fun Any.toast(msg: String = "", resId: Int = 0) {
    var toast: ToastHelper
    try {
        toast = ToastHelper.getInstance()
    } catch(e: Exception) {
        ToastHelper.init(MyApplication.myApplicationContext)
        toast = ToastHelper.getInstance()
    }
    Observable
            .just("")
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                if (TextUtils.isEmpty(msg))
                    toast.showToast(resId)
                else if (resId == 0)
                    toast.showToast(msg)
            }
}

/**
 * 通用的HtmlFormat
 */
fun String.fromHtml(): Spanned {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        return Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
    } else {
        return Html.fromHtml(this)
    }
}


fun View.expandViewTouchDelegateTo48dp(needReactWidth: Float = 48f) {
    this.post {
        val parent = parent as? View
        val suggest = CommonUtils.dp2px(context, needReactWidth)
        val realWidth = right - left
        val realHeight = bottom - top
        var isDoExpand = false
        val rect = android.graphics.Rect()
        if (suggest > realWidth) {
            val needExpand = suggest.minus(realWidth).div(2)
            rect.left = left - needExpand
            rect.right = right + needExpand
            isDoExpand = true
        }

        if (suggest > realHeight) {
            val needExpand = suggest.minus(realHeight).div(2)
            rect.top = top - needExpand
            rect.bottom = bottom + needExpand
            isDoExpand = true
        }
        if (isDoExpand)
            parent?.touchDelegate = android.view.TouchDelegate(rect, this)
    }
}

fun Any.createPartFromString(descriptionString: String): RequestBody {
    return RequestBody.create(
            okhttp3.MultipartBody.FORM, descriptionString)
}

fun createPartFromFile(file: File?,partName: String): MultipartBody.Part? {
    if (file?.isFile ?: false) {
        val requestFile = RequestBody.create(okhttp3.MultipartBody.FORM,
                file)
        return MultipartBody.Part.createFormData(partName, file?.getName(), requestFile)
    } else {
        return null
    }
}
fun createImageFromFile(file: File?,partName: String): MultipartBody.Part? {
    if (file?.isFile ?: false) {
        val requestFile = RequestBody.create(okhttp3.MultipartBody.FORM,
                file)
        return MultipartBody.Part.createFormData(partName, file?.getName(), requestFile)
    } else {
        return null
    }
}

fun Any.createPartFromByteArray(partName: String, byteArray: ByteArray?): MultipartBody.Part? {
    val requestBody = RequestBody.create(okhttp3.MultipartBody.FORM,
            byteArray)
    return MultipartBody.Part.createFormData(partName, null, requestBody)
}

fun Any.createRequestBodyByMap(map: Map<String, String>): RequestBody {
    return RequestBody.create(MediaType.parse("application/json; charset=UTF-8"), Gson().toJson(map))
}

fun BaseModel<*>?.isOk(): Boolean = this?.code == "000001"?: CommonApiError(this?.code!!)

fun CommonApiError(errorCode: String) {
    if (errorCode == "401" && errorCode == "403") {
        MainActivity.mianActivity?.finish()

    }
}
